package com.lhl.jwt.service;

import com.lhl.jwt.domain.dto.JwtUser;
import com.lhl.jwt.domain.entity.SysUser;
import com.lhl.jwt.repository.UserRepository;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @className: com.lhl.jwt.service.UserDetailsServiceImpl
 * @description: TODO 类描述
 * @author: king
 * @date: 2020-11-18
 **/
@Api(tags = "UserDetailsServiceImpl", produces = "")
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserDetailsServiceImpl implements UserDetailsService {
    private final UserRepository userRepository;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = userRepository.findByUsername(username)
                .orElseThrow(() -> new UsernameNotFoundException("User Not Found with username: " + username));
        return JwtUser.build(sysUser);
    }
}
